#############################
##AutomaticUploader	    #
##Script Bash by Benpro     #
##Mail : benpro82@gmail.com #
##Version : 0.1		    #
##Last Revision : 08/01/11  #
##Licence : GPLv3	    #
#############################
#Varialbes de conf
loginMU=""
passMU=""
loginGS=""
passGS=""
loginMir=""
passMirFTP=""
passMir=""
loginNya=""
passNya=""
nyaaSite="http://www.nyaa.eu"
fileToUpload="$1"
torrent="$1".torrent
savedlinks="/home/animesost/tools/torrents/downloaded/savedlinks.txt"
description="Uppé par AutomaticUploader by Benpro pour MO-F"
useragent="Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101230 Firefox/3.6.13"
python="/usr/local/bin/python3.1"
pycreatetorrent="/home/animesost/tools/py3createtorrent.py"
stockTorrent="/home/animesost/tools/torrents"

echo "Welcome to AutomaticUploader V0.1"
if [ -z "$1" ]; then
  echo "Il manque le fichier à uploader."
  echo "$0 nom_du_fichier"
  exit 1
fi

saveLink()
{
  service=$1
  link=$2
  echo "$fileToUpload ($service) --> $link" >> $savedlinks
}

MUtestnumber() {
  number=$(echo $RANDOM | cut --bytes=1,2,3)
  while test $number -gt 1500 -o $number -lt 0
  do number=$(echo $RANDOM | cut --bytes=1,2,3); done;
  echo "On test le serveur n°"$number" pour voir si il accepte les posts"
  verifyserver=$(ping www$number.megaupload.com -c 1 | egrep --only-matching "hosted.by.cirn.net")
  if [ -z "$verifyserver" ]; then
    MUupload $number
  else
    MUtestnumber
  fi
}

MUupload() {
  connex=$(curl -s "www.megaupload.com/?c=login" -c MuCookies --data-urlencode login=1 --data-urlencode username=$loginMU --data-urlencode password=$passMU)
  user=$(cat MuCookies | grep user | awk '{print $7}')
  id=$(cat MuCookies | grep user | awk '{print $5}')
  server="www$number.megaupload.com"
  adr="upload_done.php?UPLOAD_IDENTIFIER=$id&user=$user"
  
  echo "Serveur numero : $number - $server"
  echo Muploading ...
  link=$(curl -# --user-agent "Shockwave Flash" "$server/$adr" --form "Filename=$fileToUpload" --form-string "message=Uploaded by BashMUploader -- by benpro" --form "Filedata=@$fileToUpload;type=application/octet-stream" --form-string "user=$user" -b MuCookies)
  link=$(echo $link | egrep --only-matching "http://www.megaupload.com/[^']*") #awk '{print $17}' | sed s"/'//" | sed s"/'//" | sed s"/;//") 
  echo "Voici le lien: $link"
  saveLink "MegaUpload" "$link"
  if [ -z "$link" ]; then
    echo "Erreur on reupload ;)"
    MUtestnumber
  fi
}

MirUpload()
{
  echo Miroriing...
  ncftpput -u "$loginMir" -p "$passMirFTP" www.mirorii.com . $fileToUpload
  curl -# http://www.mirorii.com/verification.php -d utilisateur="$loginMir" -d passe="$passMir" -c MirCookies
  getlink=$(curl -# http://www.mirorii.com/list_ftp.php -b MirCookies | grep http:// | awk -F '>' '{ print $2 }')
  saveLink "Mirorii" "$getlink"
}

NyaUpload()
{
  echo "Nyaa desuing~"
  $python $pycreatetorrent $fileToUpload "http://nyaatorrents.info:3277/announce"
  curl -# "$nyaaSite/?page=login" -A "$useragent" -d loginusername="$loginNya" -d loginpassword="$passNya" -c NyaCookies -o /dev/null
  link=$(curl "$nyaaSite/?page=upload" -A "$useragent" -F name="" -F "torrent=@$torrent;type=application/x-bittorrent" -F torrenturl="" -F catid="1_38" -F info="http://mo-fansub.fr/" -F description="None" -F rules="1" -F submit="Upload" -b NyaCookies | grep "View your torrent" | awk -F '\"' '{ print $4 }' | sed 's/\&amp\;/\&/')
  mv "$torrent" "$stockTorrent"
  saveLink "Torrent" "$link"
}

GSupload()
{
  echo "GigaSuzing :o"
  curl -# gigasize.com/login.php -A "$useragent" -d uname=$loginGS -d passwd=$passGS -d d=Login -d login=1 -c GsCookies #Tention au @ du login
  token=$(curl -# gigasize.com/myfiles.php -A "$useragent" -b GsCookies | grep /up.php | awk -F '=' '{ print $4 }' | sed 's/\&amp.*//')
  curl -# "gigasize.com/up.php?sid=$token&description=&title=" -A "$useragent" -F UPLOAD_IDENTIFIER="$token" -F x="78" -F y="5" -F file_1=";type=application/octet-stream" -F "file_0=@$fileToUpload;type=application/octet-stream" -F titlel="" -F description="" -F accept=1 -b GsCookies
  link1=$(curl "http://gigasize.com/myfiles.php?order=fil_creationdate&direction=desc" -b GsCookies | grep -m 1 get.php | awk -F "\"" '{print $2 }' | sed 's/http:\/\///')
  link2=$(python -c "import urllib; print urllib.quote('''$link1''')")
  link3=$(curl $link2 | grep -m 1 get.php | awk -F "'" '{ print $2 }')
  saveLink "GigaSize" "$link3"
}

RestartDeluge()
{
  echo "Restartation de deluged :)"
  killall -9 deluged
  deluged &
}

MUtestnumber
MirUpload
GSupload
NyaUpload
RestartDeluge
# exit 1
